package com.example.nettyrpcdemo.core;

import cn.hutool.extra.spring.SpringUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * 服务提供者收到消息
 */
@Slf4j
public class RpcHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg == null) {
            return;
        }

        ProtobufMessage message = (ProtobufMessage) msg;

        // 反射调用
        SpringUtil.getBean(RpcService.class).callProviderMethod(ctx.channel(), message);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // 如：远程关闭之类的，暂时就不做处理了
        //log.error("error=", cause);
    }
}
